layui.use(['form', 'layer', 'formSelects'], function () {
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        formSelects = layui.formSelects,
        $ = layui.jquery;


    var zTreeObj;
    $(function () {
        loadModuleInfo();
    });

    function loadModuleInfo() {
        $.ajax({
            type: "post",
            url: ctx + "/module/findModules2?roleId=" + $("#roleId").val(),
            dataType: "json",
            success: function (data) {
                // zTree 的参数配置，深⼊使⽤请参考 API ⽂档（setting 配置详解）
                var setting = {
                    data: {
                        simpleData: {
                            enable: true
                        }
                    },
                    view: {
                        showLine: false
                        // showIcon: false
                    },
                    check: {
                        enable: true,
                        chkboxType: {"Y": "ps", "N": "ps"}
                    },
                    callback: {
                        onCheck: zTreeOnCheck
                    }
                };
                var zNodes = data;
                zTreeObj = $.fn.zTree.init($("#test1"), setting, zNodes);
            }
        })
    }

    function zTreeOnCheck(event, treeId, treeNode) {
        var j = 0;
        j++;
        $(".permit_btn").click(function () {
            console.log("授权")
            var nodes = zTreeObj.getCheckedNodes(true);
            var roleId = $("#roleId").val();
            var mids = "mids=";
            for (var i = 0; i < nodes.length; i++) {
                if (i < nodes.length - 1) {
                    mids = mids + nodes[i].id + "&mids=";
                } else {
                    mids = mids + nodes[i].id;
                }
            }
            j--;
            $.ajax({
                type: "post",
                url: ctx + "/role/addGrant",
                data: mids + "&roleId=" + roleId,
                dataType: "json",
                success: function (result) {
                    console.log(typeof result + "-->" + result.code + "-->" + result.msg);
                    if (result.code == 200) {
                        if (j == 1) {
                            var succ = result.code;
                        }
                    } else {
                        alert("授权失败");
                    }
                }
            });
            if (succ = 200) {
                var index = top.layer.msg('数据提交中，请稍候', {icon: 16, time: false, shade: 0.8});
                parent.location.reload();
                layer.msg("授权成功")
            }
        })
    }
})


